package bookstudytest.Exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Scanner;

/**
 * Created by swordbone on 2017/12/12.
 * 一种更为简便的关闭资源的方法
 */
public class TryCatch {

    public static void main(String[] args){
//        这种方式可以让try后括号内打开的资源在使用完毕的时候自动关闭
        try (Scanner scanner = new Scanner(System.in);PrintWriter pw = new PrintWriter("C:/code/cc.txt"))
        {
            while (scanner.hasNext()){
                System.out.println(scanner.next());
                pw.println(scanner.next().toUpperCase());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//        测试值栈追踪方法
        TryCatch.factorial(5);
    }

    public static int factorial(int a){
        System.out.println("factorial(" + a + ");");
        Throwable throwable = new Throwable();
        StackTraceElement[] frames = throwable.getStackTrace();
        for (StackTraceElement stackTraceElement : frames){
            System.out.println(stackTraceElement);
        }
        int b;
//      这种循环是这种结构[[[[[最里面的循环]]]]]
        if (a <= 1){
            b = 1;
        }else{
            b = a * factorial(a - 1);
        }
        System.out.println("return " + b);
        return b;
    }


}
